//
//  BaseViewController.m
//  PerfectProject
//
//  Created by Meng huan on 14/11/19.
//  Copyright (c) 2014年 M.H Co.,Ltd. All rights reserved.
//

#import "BaseViewController.h"
#import "MBProgressHUD.h"
@interface BaseViewController ()
{
    MBProgressHUD* mbProgressHud;
}
@end

@implementation BaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Do any additional setup after loading the view.
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
    // iOS7顶部屏幕适配
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
#endif
    
    
    // 默认背景色
    self.view.backgroundColor = DEF_RGB_COLOR(59, 172, 252);
    
    // 导航栏左按钮
    _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftButton.contentEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 30);
    _leftButton.frame = CGRectMake(0,0,40,40);
//    [_leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [_leftButton addTarget:self action:@selector(leftNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIImageView * imageIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 20, 20)];
    imageIV.image = [UIImage imageNamed:@"left_iv"];
    imageIV.contentMode = UIViewContentModeScaleAspectFit;
    [_leftButton addSubview:imageIV];
    UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:_leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    // 内容视图
    self.contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_HEIGHT(self.view)-DEF_TAB_BAR_HEIGHT)];
    self.contentView.showsVerticalScrollIndicator = NO;
    self.contentView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    [self.view addSubview:self.contentView];
    
    //手势
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    
    
}
#pragma mark - Public Methods
- (void)addSubview:(UIView *)view
{
    [self.contentView addSubview:view];
}

#pragma mark - 软键盘
-(void)keyboardHide:(UITapGestureRecognizer *)tap
{
    [self.view endEditing:YES];
}
#pragma mark - 点击事件
- (void)leftNavItemClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 错误提示

- (void)errorTipHUDByMsg:(NSString *)msg
{
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"提示"
                                                 message:msg
                                                delegate:nil
                                       cancelButtonTitle:@"确定"
                                       otherButtonTitles:nil, nil];
    [av show];
}

#pragma mark HUD
-(void)showHUDWithString:(NSString *)title
{
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:appDelegate.window animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.margin = 10.f;
    hud.labelFont = [UIFont systemFontOfSize:12];
    hud.yOffset = 250.f;
    if (DEF_SCREEN_HEIGHT < 568)
    {
        hud.yOffset = 200;
    }
    [appDelegate.window addSubview:hud];
    [hud.superview bringSubviewToFront:hud];
    hud.labelText = title;
    hud.userInteractionEnabled = NO;
    [hud show:YES];
    [hud hide:YES afterDelay:2.0];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -
#pragma mark - 支持重力感应，屏幕旋转
- (BOOL)shouldAutorotate
{
    // 开启自动旋转，通过supportedInterfaceOrientations来指定旋转的方向
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    // 指定旋转的方向
    
    return UIInterfaceOrientationMaskAll;
//    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
//    return UIInterfaceOrientationMaskPortrait;
}

@end
